package zunge.lbe.analysis;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import zunge.tools.DbConn;

public class Analysis_Clustering {
	
	DataSet dataSet;
	String libFolder;
	
	String folder;
	
	public Analysis_Clustering(DataSet dataSet, String libFolder){
		this.dataSet = dataSet;
		this.libFolder = libFolder;
		this.folder = dataSet.folder + "clustering/";
	}
	
	public void gen(Analysis_Social as) throws Exception{
		new File(this.folder).mkdir();
		
		List<String> scripts = new ArrayList<String>();
		
		//Clustering would be based on [u,p] feature matrix
		Irlba irlba_up = new Irlba(as.folder, "svd_up_global");
		String f_uf = irlba_up.getFile(Irlba.fsuf_uf);
		
		int k=1;
		//for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			scripts.add(kmeans.genScript(libFolder, f_uf));
		//}
		
		ScriptMgr.splitScript(scripts, 6, folder+"cmd_kmeans.sh");
	}
	
	public void genReport(DbConn conn, Analysis_Social as) throws Exception{
		Irlba irlba_up = new Irlba(as.folder, "svd_up_global");
		String f_matrix = irlba_up.getFile(Irlba.fsuf_matrix);
		
		int k=1;
		//for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			kmeans.genReport(conn, f_matrix, dataSet);
		//}
	}
	public void genHeatMap(DbConn conn, Analysis_Social as) throws Exception{
		Irlba irlba_up = new Irlba(as.folder, "svd_up_global");
		String f_matrix = irlba_up.getFile(Irlba.fsuf_matrix);
		
		for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			kmeans.genHeatMap(f_matrix, dataSet);
		}
	}
	public void genQA(DbConn conn, Analysis_Social as) throws Exception{
		//TODO
		Irlba irlba_up = new Irlba(as.folder, "svd_up_global");
		String f_matrix = irlba_up.getFile(Irlba.fsuf_matrix);
		
		for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			kmeans.genQA(f_matrix, dataSet);
		}
	}
	
	
	
	public void test() throws Exception{
		List<String> scripts = new ArrayList<String>();
		
		int k=1;
		//for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			scripts.addAll(kmeans.genTest(libFolder, dataSet));
		//}
		
		ScriptMgr.splitScript(scripts, 12, folder+"cmd_kmeans_test.sh");
	}
	
	public void collectTest() throws Exception{

		int k=1;
		//for(int k = 1;k <= KMeans.K;++k){
			KMeans kmeans = new KMeans(folder, "kmeans_k"+k, k);
			kmeans.getTestResult(libFolder, dataSet);
		//}
	}
	
}
